<template>
  <div class="selectInput">
    <el-input
     v-model="inputValue"
      type="text"
      style="width: 100%"
      placeholder="请选择口味"
      clearable
      readonly
      @focus="selectFlavor(true)"
      @blur="outSelect(false)"
    />
    <div v-if="mak && snacksFlavorsData.length" class="flavorSelect">
      <span
        v-for="(it, ind) in snacksFlavorsData"
        :key="ind"
        class="items"
        @click="checkOption(it, ind)"
        >{{ it.name }}</span
      >
      <span v-if="snacksFlavorsData == []" class="none">无数据</span>
    </div>
  </div>
</template>

<script setup>
import { ref, defineProps, defineEmits, watch } from 'vue'



const props = defineProps({
  selectFlavorsData: { default: [] },
  snacksFlavorsData: { default: [] },
  value: { default: '' },
  index: { default: 0 }
})

const mak = ref(false)
const inputValue = ref(props.value)
const  emit  = defineEmits(['select'])

const selectFlavor = (st) => {
  mak.value = st
}

const outSelect = (st) => {
  setTimeout(() => {
    mak.value = st
  }, 200)
}

const inputHandle = (val) => {
  selectFlavor(false)
}

const checkOption = (val, ind) => {
  emit('select', val.name, props.index, ind)
}

watch(() => props.value, (newVal, oldVal) => {
  inputValue.value = newVal
})

</script>

<style lang="scss" scoped>
.selectInput {
  position: relative;
  width: 100%;
  min-width: 100px;
  .flavorSelect {
    position: absolute;
    width: 100%;
    border-radius: 3px;
    border: solid 1px #e4e7ed;
    line-height: 30px;
    text-align: center;
    background: #fff;
    top: 50px;
    z-index: 99;
    .items {
      cursor: pointer;
      display: inline-block;
      width: 100%;
      line-height: 35px;
      border-bottom: solid 1px #f4f4f4;
      color: #666;
      margin: 0 !important;
      &:hover {
        background-color: #fffbf0;
      }
      &:active {
        background-color: #fffbf0;
        color: #ffc200;
      }
    }
    .none {
      font-size: 14px;
    }
  }
}
</style>
